package zookeeper.com.ksfzhaohui.zookeeper;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.zookeeper.KeeperException;

public class Test {
	public static final Logger logger = Logger.getLogger(Test.class);

	private static final int times = 5;

	public static void main(String[] args) throws InterruptedException,
			KeeperException, IOException {
		//		final Map<Integer, DistributedLock> lockMap = new HashMap<Integer, DistributedLock>();
		//		for (int i = 0; i < times; i++) {
		//			lockMap.put(i, new DistributedLock("127.0.0.1:2181"));
		//		}
		//
		//		Thread.sleep(5000);
		final DistributedLock distributedLock = new DistributedLock(
				"127.0.0.1:2181");
		Thread.sleep(1000);
		for (int k = 0; k < times; k++) {
			//			final int temp = k;
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						//						for (int i = 0; i < times; i++) {
						long endTime = 0;
						//							DistributedLock distributedLock = lockMap.get(temp);
						for (int i = 0; i < 10; i++) {
							long startTime = System.currentTimeMillis();
							try {
								distributedLock.lock();
							} catch (Exception e) {
								e.printStackTrace();
							} finally {
								try {
									distributedLock.unlock();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							endTime = System.currentTimeMillis();
							System.err
									.println("获取分布式锁的时间:(endTime-startTime)=【"
											+ (endTime - startTime) + "】ms");
						}
						//						}
					} catch (Exception e) {
						logger.error(e);
					}
				}
			}).start();
		}
	}
}
